When you query records in a database,
your query can often return hundreds, thousands or even millions of results
depending on how much data there is. But you might only require
a fraction of these results. As you saw in earlier courses,
you can use filtering and sorting techniques to target only
the data you need from these results. When querying a MySQL database using
Python, you can apply these same techniques to ensure that your
query target specific data. Over the next few minutes,
you'll recap some basic filtering and sorting techniques and learn how to
filter and sort MySQL data using Python. Over at Little Lemon, the restaurant is
currently querying the bill records of each customer in its database. They need the records of all bookings with
a final bill greater than or equal to $40. They also need the bookings to appear in
ascending order with respect to the total bill amount. Filtering and sorting techniques
are a great way to carry out this task. Let's find out how these techniques work
with Python and then help Little Lemon. One filtering technique that you should
be familiar with is the use of the WHERE clause. Let's begin with a quick
recap of the WHERE clause. As you should know by now,
you can use this clause to filter and extract records that satisfy
a specific condition. For example, Little Lemon can use
the WHERE clause to create a SQL select statement that targets the booking ID and
bill amount columns in their orders table. The statement returns all values in the
bill amount column equal to a value of 40. The WHERE clause helps to filter
the records from the database but not to the extent that
Little Lemon require. As you saw earlier,
Little Lemon need the records of all bookings with the final bill
greater than or equal to $40. You can use comparison operators to
specify the exact data you require from a database. For example, Little Lemon can
replace the equals operator in their statement with the greater than or
equals to comparison operator. This operator targets all records from a
database where the bill amount is greater than or equal to a value of 40. This narrows down
the records even further, returning much more specific results. Another technique is the ORDER BY clause. As you should know, the ORDER BY clause is
an optional clause that can be added to a sequel select statement. It helps to sort data in ascending or
descending order. So to filter their records
even more efficiently, Little Lemon can add the ORDER BY
clause to the end of their statement. They target the bill amount column and
then type the ascending keyword. Once executed, the query returns
all bill amounts greater than or equal to $40 in ascending order. Now that you've recapped some
examples of filtering and sorting techniques,
your next question might be how can I use these techniques in
Python to query a MySQL database? Well as you saw earlier, Little Lemon need to query the records
in their MySQL database using python. Let's help them create this
query using the filtering and sorting techniques that you just explored. To recap, Little Lemon need the records
of all bookings with a final bill greater than or equal to $40. They also need the bookings
to appear in ascending order with respect to
the total bill amount. First, write a sequel select query
as a Python string stored in a variable called my_sql_query. The query must target the booking ID and
bill amount columns from the orders table. Use the WHERE clause and a greater than or
equal to comparison operator to target all records that are greater
than or equal to the value of 40. Then use the ORDER BY clause and
the ascending keyword to order all records from the bill
amount column in ascending order. The next step is to establish a connection
through the MySQL connector Python API between the front end Python application
and the back end MySQL database. Then get the cursor object from
the connection using the cursor method. Run the query using the execute
method from the cursor object. Once the query is successfully executed,
you can fetch all the results in a new variable called results that satisfies
the conditions given in your query. Just use the fetch all method on the
cursor object to grab the query results. All the retrieved data is in results
as a Python list of tuples. You can display the list on your
dashboard for Little Lemon. Little Lemon now have the data they need
and you should now be able to explain
how to make use of filtering and sorting techniques to target records
in the MySQL database using Python. Great work.